<template>
  <div class="login">
    <div class="nav">
      <h1>后台管理系统</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input  prefix-icon="el-icon-s-custom" v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input show-password  prefix-icon="el-icon-lock" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dl()">登录</el-button>
        <el-button type="info"  @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    </div>
  </div>
</template>

<script>
import {LoginApi} from  '../../http/api'
export default {
  name:'login',
  data() {
    return {
      ruleForm: {
        username: 'admin',
        password:'123456',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请选择密码', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
      dl(){
        this.$refs.ruleForm.validate(async valid => {
          if(!valid) return false

          await LoginApi(this.ruleForm)

          this.$router.push('/home')
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>

<style>
  .login{
    width: 100%;
    height: 100vh;
    background-color: #2b4b6b;
    position: relative;
  }
  .nav{
    width: 35%;
    height: 40%;
    background-color: #fff;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
  }
  h1{
    margin-left: 120px;
  }
  .el-input{
    width: 350px;
  }
</style>